
import { reqGetCityList } from '@/api'
let defaultCity = '上海'
try {
  if (localStorage.city) {
    // eslint-disable-next-line no-unused-vars
    defaultCity = localStorage.city
  }
} catch (e) {}

const state = {
  cityList: {},
  city: defaultCity
}

const mutations = {
  GETCITYLIST(state, cityList) {
    state.cityList = cityList
  },
  CHANGECITY(state, city) {
    state.city = city

    try {
      localStorage.city = city
    } catch (e) {}
  }
}

const actions = {
  async getCityList({ commit }) {
    // eslint-disable-next-line prefer-const
    let result = await reqGetCityList()
    console.log(result)
    if (result.code === 200) {
      commit('GETCITYLIST', result.data)
    }
  },
  changeCity({ commit }, bro) {
    commit('CHANGECITY', bro)
  }
}

const getters = {
  cities(state) {
    return state.cityList.cities || {}
  },
  hotCities(state) {
    return state.cityList.hotCities || []
  }
}
export default {
  state,
  mutations,
  actions,
  getters
}
